package com.jiezheng.huinong.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.jiezheng.huinong.db.conf.IPaperManageDAO;
import com.jiezheng.huinong.model.PaperInfo;
import com.jiezheng.huinong.util.DbUtil;

public class PaperManageDAOImpl implements IPaperManageDAO {

	/**
	 * 增加文号
	 */
	@Override
	public void addPaper(PaperInfo paper) {
		
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.paperManage.addPaper", paper);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	
	/**
	 * 删除文号
	 */
	@Override
	public void deletePaper(String paperName) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.paperManage.deletePaper", paperName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	
	/**
	 * 查询文号
	 */
	@Override
	public PaperInfo getPaperByPaperNo(String paperNo) {
		
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		PaperInfo paper = null;
		try {
			paper = session.selectOne("com.jiezheng.huinong.paperManage.getPaperByPaperNo", paperNo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return paper;
	}

	@Override
	public void modifyPaper(PaperInfo paper) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.paperManage.modifyPaper",
					paper);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	

	@Override
	public List<PaperInfo> getPaperInfo(String cond, Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<PaperInfo> papers = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("cond", cond);
		
		try {
			papers = session.selectList(
					"com.jiezheng.huinong.paperManage.getPaperInfoByPage", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return papers;
	}

	@Override
	public Integer getTotalPaperNum(String cond) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.paperManage.getTotalPaperNum", cond);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<String> listIndexNo() {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<String> data = null;
		
		try {
			data = session.selectList(
					"com.jiezheng.huinong.paperManage.listIndexNo");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return data;
	}

	/**
	 * 取得未被占用的文号
	 * **/
	@Override
	public List<String> getNoUsedIndexNo() {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<String> indexes = null;
		try {
			indexes = session
					.selectList("com.jiezheng.huinong.paperManage.getNoUsedIndexNo");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexes;
	}

}
